/* =============================== 元素标签 =============================== */
import type {
  ElementTagConfig,
  ProcessedElementTagConfig,
  TagCategoriesConfig,
} from '../../types/constants'
import type { PlatformTypeValue } from '../../types/interfaces'
import {
  createElementTagConfig,
  ElementSelectorType,
  HEADING_TAG,
  HOSTNAME,
  isPlatformCategoryType,
  PlatformCategoryType,
  SUPPORTED_PLATFORMS,
} from '../../types/constants'
import { ELEMENT_IDENTIFIER_CONFIG } from '../service/element_identifier/types'

const category: string = ELEMENT_IDENTIFIER_CONFIG.blog.article.id

export class ElementTag {
  static instance: ElementTag
  private readonly DEFAULT_CONFIG: Record<typeof category, ElementTagConfig> = {
    [category]: {
      show: false,
      hide: false,
      selectorType: ElementSelectorType.Tag,
      tag: 'article',
    },
  }

  // 创建包含所有平台的默认配置
  private readonly CONFIG: TagCategoriesConfig = Object.fromEntries(
    Object.values(SUPPORTED_PLATFORMS).map(platform => [platform, this.DEFAULT_CONFIG]),
  )

  static getInstance(): ElementTag {
    if (!ElementTag.instance)
      ElementTag.instance = new ElementTag()
    return ElementTag.instance
  }

  getConfigs(): TagCategoriesConfig {
    return this.getConfig()
  }

  getSelectors(options?: { hostname?: string, ignorePlatformCheck?: boolean }): ProcessedElementTagConfig[] {
    const { hostname = HOSTNAME, ignorePlatformCheck = false } = options || {}

    return Object.entries(this.getConfig())
      .filter(([platform]) => !ignorePlatformCheck && platform === hostname)
      .reduce((result, [_, configData]) => {
        return result.concat(
          Object.entries(configData).map(([identifier, config]) =>
            createElementTagConfig(identifier, config),
          ),
        )
      }, [] as ProcessedElementTagConfig[])
  }

  getSelector(name_zh: string): ProcessedElementTagConfig | undefined {
    return Object.values(this.getSelectors()).find(
      config => config.nameZh === name_zh,
    )
  }

  getConfig(): TagCategoriesConfig {
    const notBlogPlatforms: PlatformTypeValue[] = Object.values(SUPPORTED_PLATFORMS)
      .filter(platform => !isPlatformCategoryType(platform, PlatformCategoryType.BLOG))

    // 只覆盖需要特殊处理的平台
    Object.assign(this.CONFIG, {
      [SUPPORTED_PLATFORMS.JIAN_SHU]: {
        [category]: {
          ...this.DEFAULT_CONFIG[category],
          tag: 'section:has(> article)',
          hide_parent_siblings: true, // 隐藏相邻兄弟元素
        },
      },
      [SUPPORTED_PLATFORMS.JUE_JIN]: {
        [category]: {
          ...this.DEFAULT_CONFIG[category],
          tag: '.article-area',
        },
      },
      [SUPPORTED_PLATFORMS.GITEE]: {
        [category]: {
          ...this.DEFAULT_CONFIG[category],
          tag: `blob-markdown-renderer:has(> :is(${HEADING_TAG}))`,
          listen: true,
        },
      },

      // 特殊处理
      ...Object.fromEntries(notBlogPlatforms.map(platform => [platform, {}])),
    })
    return this.CONFIG
  }
}
